#include "global.h"
#include "level.h"

void init();
void deinit();

ALFONT_FONT *technicality1 = NULL;

BITMAP *titleBG;
BITMAP *tiles;

//TIMER

volatile int ticks = 0;

void ticker()
{
    ticks++;
}
END_OF_FUNCTION(ticker)

//END TIMER


int main() {
	init();
    alfont_init();
    set_window_title("Cubicle");

    global Globals;
    Globals.init();

    bool quit = false;

    //Load TTF Fonts
    //This requires AlFont (I use 1.9.1)
    technicality1 = alfont_load_font("technicality1.ttf");
    alfont_set_font_size(technicality1, 20);

    //Create our dear double buffer thing



    titleBG = load_bitmap("gfx/title.bmp", NULL);
    tiles = load_bitmap("gfx/tiles.bmp", NULL);

    if (titleBG == NULL)
    {
        allegro_message("Failed to load gfx! Check for missing files.");
        quit = true;
    }

    /*
    int TILE_CUBEWALL = makecol(255,255,255);
    int TILE_RCOOLER = makecol(0, 200, 200);
    int TILE_LCOOLER = makecol(100, 200, 200);
    int TILE_LWORK = makecol(128, 64, 0);
    int TILE_RWORK = makecol(128, 64, 64);
    int TILE_SLWORK = makecol(255, 64, 0);
    int TILE_SRWORK = makecol(255, 128, 0);
    */

    int COLOR_GROUND = getpixel(tiles,0,0);
    destroy_bitmap(tiles);

    enum
    {
        STATE_MENU,
        STATE_GAME,
        STATE_OVER,
    };

    unsigned char gameState = STATE_MENU; //Unsigned 8-bit int ftw!
    unsigned char oldState = STATE_MENU;

    int menuSelection = 0;
    int menuTimer = 0;

    Level *TestLevel = new Level("levels/testLevel.bmp");
    if (TestLevel->isReady == false)
    {
        allegro_message("Failed to load testlevel.bmp!");
        return -1;
    }

	while (!quit)
	{
        //allegro_message("The crash is in the main loop.");
	    while (ticks == 0)
	    {
	        rest (100 / 60);
	    }


	    while (ticks > 0)
	    {
	        int old_ticks = ticks;
            //GAME LOGIC, GO!
            if (gameState == STATE_MENU)
            {
                if (oldState == STATE_GAME)
                {
                    oldState = STATE_MENU;
                    menuTimer = 0;
                }

                if (key[KEY_ESC] && menuTimer > 20) quit = true;

                menuTimer++;
                if ((key[KEY_DOWN]) && menuTimer > 20) { menuSelection++; menuTimer = 0; }
                if ((key[KEY_UP]) && menuTimer > 20) { menuSelection--; menuTimer = 0; }

                if (menuSelection >= 2) menuSelection = 0;
                if (menuSelection <= -1) menuSelection = 1;

                if (menuTimer > 10 && menuSelection == 1 && key[KEY_ENTER]) quit = true;
                if (menuTimer > 10 && menuSelection == 0 && key[KEY_ENTER]) gameState = STATE_GAME;
            }

            else if (gameState == STATE_GAME)
            {
                if (key[KEY_ESC]) { gameState = STATE_MENU; oldState = STATE_GAME;}
            }
            else if (gameState == STATE_OVER)
            {
                //DEATH (and/or other events leading to game over)
            }

            ticks--;
            if (old_ticks <= ticks)
                break;
        }
            //DRAWING, GO!
        if (gameState == STATE_MENU)
        {
            draw_sprite(Globals.BUFFER, titleBG, 0, 0);
            alfont_textout_ex(Globals.BUFFER, technicality1, "start", (int) 210, (int) SCR_H/2 - 22, makecol(255, 255, 255), -1);
            alfont_textout_ex(Globals.BUFFER, technicality1, "quit", (int) 210, (int) SCR_H/2, makecol(255, 255, 255), -1);

            if (menuSelection == 0)
            {
                triangle(Globals.BUFFER, 195, SCR_H/2 -17, 205, SCR_H/2 -12, 195, SCR_H/2 - 7, makecol(255,255,255));
            }

            else if (menuSelection == 1)
            {
                triangle(Globals.BUFFER, 195, SCR_H/2 +5, 205, SCR_H/2 +10, 195, SCR_H/2 +15, makecol(255,255,255));
            }

        }

        if (gameState == STATE_GAME)
        {
            //Draw the floor; easier this way, and causes less problems.
            clear_to_color(Globals.BUFFER, COLOR_GROUND);

            //Draw the shadowed walls, so objects can walk in front of them
            TestLevel->drawShadows();

            //Now, let's draw all the other objects.
            //They'll pass in front of walls, but behind tiles, to allow for fake perspective


            //Here come the tiles
            TestLevel->drawTiles();

        }

        stretch_sprite(screen, Globals.BUFFER, 0, 0, SCR_W*2, SCR_H*2);
	}

    delete TestLevel;
    alfont_exit();
	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCR_W*2, SCR_H*2, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();

	LOCK_VARIABLE(ticks);
	LOCK_FUNCTION(ticker);

    install_int_ex(ticker, BPS_TO_TIMER(60));

	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();

	destroy_bitmap(titleBG);
}
